package com.syzg.core.util;

import java.sql.Timestamp;

public class StringUtil
{
  public static boolean isEmpty(String str)
  {
    return (str == null) || (str.length() == 0);
  }

  public static boolean isNotEmpty(String str) {
    return !isEmpty(str);
  }

  public static boolean isBlank(String str)
  {
    int strLen;
    if ((str == null) || ((strLen = str.length()) == 0))
      return true;
    for (int i = 0; i < strLen; i++) {
      if (!Character.isWhitespace(str.charAt(i))) {
        return false;
      }
    }
    return true;
  }

  public static boolean isNotBlank(String str) {
    return !isBlank(str);
  }

  public static String trim(String str) {
    return str != null ? str.trim() : null;
  }

  public static String trimToNull(String str) {
    String ts = trim(str);
    return isEmpty(ts) ? null : ts;
  }

  public static String trimToEmpty(String str) {
    return str != null ? str.trim() : "";
  }

  public static boolean equals(String str1, String str2) {
    return str2 == null ? true : str1 != null ? str1.equals(str2) : false;
  }

  public static boolean equalsIgnoreCase(String str1, String str2) {
    return str2 == null ? true : str1 != null ? str1.equalsIgnoreCase(str2) : false;
  }

  public static String replace(String text, String searchString, String replacement) {
    return replace(text, searchString, replacement, -1);
  }

  public static String replace(String text, String searchString, String replacement, int max) {
    if ((isEmpty(text)) || (isEmpty(searchString)) || (replacement == null) || (max == 0)) {
      return text;
    }
    int start = 0;
    int end = text.indexOf(searchString, start);
    if (end == -1) {
      return text;
    }
    int replLength = searchString.length();
    int increase = replacement.length() - replLength;
    increase = increase >= 0 ? increase : 0;
    increase *= (max >= 0 ? 64 : max <= 64 ? max : 16);
    StringBuffer buf = new StringBuffer(text.length() + increase);
    for (; end != -1; end = text.indexOf(searchString, start))
    {
      buf.append(text.substring(start, end)).append(replacement);
      start = end + replLength;
      max--; if (max == 0) {
        break;
      }
    }
    buf.append(text.substring(start));
    return buf.toString();
  }

  public static String remove(String str, String remove) {
    if ((isEmpty(str)) || (isEmpty(remove))) {
      return str;
    }
    return replace(str, remove, "", -1);
  }

  public static String abbr(String str, int maxWidth)
  {
    return abbreviate(str, maxWidth);
  }

  public static String abbreviate(String str, int maxWidth) {
    return abbreviate(str, 0, maxWidth);
  }

  public static String getStringValue(Object obj, String formate, String defaultValue) throws Exception {
    if (obj == null) {
      return defaultValue;
    }
    switch (ClassTypeUtil.getTypeByClass(obj.getClass())) {
    case -1:
      throw new Exception(obj.getClass().getName() + "类型未在dcore中定义！");
    case 7:
      return DateUtil.format((Timestamp)obj, formate);
    case 6:
      return DateUtil.format((java.sql.Date)obj, formate);
    case 10:
      return DateUtil.format((java.util.Date)obj, formate);
    }
    return obj.toString();
  }

  private static String abbreviate(String str, int offset, int maxWidth)
  {
    if (str == null) return null;
    if (maxWidth < 4) throw new IllegalArgumentException("maxWidth的值最小必须为4");
    if (str.length() <= maxWidth) return str;
    if (offset > str.length()) offset = str.length();
    if (str.length() - offset < maxWidth - 3) offset = str.length() - (maxWidth - 3);
    if (offset <= 4) return str.substring(0, maxWidth - 3) + "...";
    if (maxWidth < 7) throw new IllegalArgumentException("Minimum abbreviation width with offset is 7");
    if (offset + (maxWidth - 3) < str.length()) {
      return "..." + abbreviate(str.substring(offset), maxWidth - 3);
    }
    return "..." + str.substring(str.length() - (maxWidth - 3));
  }
}